<template>
	<view class="">
		<view class="page-box" v-if="orderList.length">
			<view class="order" v-for="(res, index) in orderList" :key="index"
				@click="$u.route('/pages/shop/order/detail/detail',{orderSn:res.orderSn})">
				<view class="top">
					<view class="left">
						<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
						<view class="store">{{ res.orderSn }}</view>
						<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
					</view>
					<view class="right">{{ res.statusName }}</view>
				</view>
				<view class="item u-flex u-row-between" v-for="(item, idx) in res.items" :key="idx">
					<view class="u-flex">
						<view class="left">
							<image :src="item.pic" mode="aspectFill"></image>
						</view>
						<view class="content">
							<view class="title u-line-2">{{ item.goods.name }}</view>
							<view class="type">{{ item.goods.remarks }}</view>
							<view class="delivery-time">付款后1天内发货</view>
						</view>
					</view>
					<view class="right">
						<view class="price">
							￥{{ item.price| getPrice }}
						</view>
						<view class="number">x{{ item.count }}</view>
					</view>
				</view>
				<view class="total u-flex u-row-between">
					<text>共{{ totalNum(res.items) }}件商品</text>
					<text>合计:
						<text class="total-price">
							￥{{ res.totalPrice | getPrice }}
						</text>
					</text>
				</view>
				<view class="total u-flex u-row-between" v-if="res.score">
					<text>使用{{res.score}}积分抵扣{{(res.proportion*res.score) | getPrice}}元</text>
					<text>实付:
						<text class="total-price">
							￥{{ (res.totalPrice-res.proportion*res.score) | getPrice }}
						</text>
					</text>
				</view>
				<view class="bottom">
					<template v-if="current==0">
						<view class="exchange btn u-m-r-20" @click.stop="delOrder(res.orderSn,index)">取消订单</view>
						<view class="evaluate btn"
							@click.stop="$u.route('/pages/shop/pay/pay',{totalPrice:(res.totalPrice-res.proportion*res.score),orderSn:res.orderSn})">
							立即付款</view>
					</template>
					<template v-if="current==1">
						<view class="evaluate btn" @click.stop="orderNotify(res.orderSn)">提醒发货</view>
					</template>
					<template v-if="current==2">
						<view class="evaluate btn" @click.stop="orderConfirm(res.orderSn,index)">确认收货</view>
					</template>
				</view>
			</view>
			<u-loadmore :status="loadStatus" bgColor="#f2f2f2"></u-loadmore>
		</view>
		<view class="page-box" v-else>
			<view>
				<view class="centre">
					<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode=""></image>
					<view class="explain">
						您还没有相关的订单
						<view class="tips">可以去看看有那些想买的</view>
					</view>
					<view class="btn" @click="goHome">随便逛逛</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		orderCancel,
		orderNotify,
		orderConfirm
	} from '@/api/shop.js'
	export default {
		name: "w-order-item",
		props: {
			orderList: {
				type: Array,
				default: () => {
					return []
				}
			},
			loadStatus: {
				type: String,
				default: ''
			},
			current: {
				type: Number,
				default: 0
			}
		},
		computed: {
			// 价格小数
			priceDecimal() {
				return val => {
					if (val !== parseInt(val)) return val.slice(-2);
					else return '00';
				};
			},
			// 价格整数
			priceInt() {
				return val => {
					if (val !== parseInt(val)) return val.split('.')[0];
					else return val;
				};
			}
		},
		data() {
			return {

			};
		},
		methods: {
			goHome(){
				this.$store.commit('setShopIndexm',0)
				this.$u.route({
					url:'/pages/shop/shop',
					type:'tab'
				})
			},
			orderConfirm(orderSn,index){
				orderConfirm({
					JSESSIONID: uni.getStorageSync('sessionid'),
					orderSn
				}).then(res => {
					this.$emit('delOrder',this.current, index)
				})
			},
			orderNotify(orderSn) {
				orderNotify({
					JSESSIONID: uni.getStorageSync('sessionid'),
					orderSn
				}).then(res => {
					this.$u.toast('提醒发货成功')
				})
			},
			delOrder(orderSn, index) {
				uni.showModal({
					title: '提示',
					content: '确定取消该订单？',
					success: (res) => {
						if (res.confirm) {
							orderCancel({
								JSESSIONID: uni.getStorageSync('sessionid'),
								orderSn
							}).then(res => {
								this.$u.toast('取消成功')
								this.$emit('delOrder',this.current, index)
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 总价
			totalPrice(item) {
				let price = 0;
				item.map(val => {
					price += parseFloat(val.price);
				});
				return price.toFixed(2);
			},
			// 总件数
			totalNum(item) {
				let num = 0;
				item.map(val => {
					num += val.count;
				});
				return num;
			},
		},
	}
</script>

<style lang="scss">
	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: flex-end;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}
</style>
